
$(function () {
    $("#jqGrid").Grid({
        url: '../groupPurchase/list',
        colModel: [
            {label: 'id', name: 'id', index: 'id',align:"center", key: true, hidden: true},
            {label: '拼团活动标题', name: 'title', index: 'title',align:"center", width: 60},
            {label: '拼团人数', name: 'personNumber', index: 'personNumber', width: 60,align:"center",},
            {label: '拼团价格', name: 'groupPurchasePrice', index: 'groupPurchasePrice', width: 60,align:"center",},
            {label: '图片', name: 'activityImg', index: 'activityImg', width: 80, formatter: function (value) {
                    return transImg(value);
                }},
            {label: '描述', name: 'description', index: 'description', width: 100},
            // {label: '商品价格', name: 'giftTypeEntity.giftContain', index: 'giftContain', width: 100},
            // {label: '拼团价格', name: 'giftTypeEntity.giftContain', index: 'giftContain', width: 100},
            {label: '拼团开始时间', name: 'activityBeginDate', index: 'activityBeginDate', width: 100,formatter: function (value){
                    var time = new Date(value);
                    var y = time.getFullYear();
                    var m = time.getMonth()+1;
                    var d = time.getDate();
                    var h = time.getHours();
                    var mm = time.getMinutes();
                    var s = time.getSeconds();
                    var add0=function (m) {
                        return m<10?'0'+m:m
                    }
                    return y+'-'+add0(m)+'-'+add0(d)+' '+add0(h)+':'+add0(mm)+':'+add0(s);
                }},
            {label: '拼团结束时间', name: 'activityEndDate', index: 'activityEndDate', width: 100,formatter: function (value){
                    var time = new Date(value);
                    var y = time.getFullYear();
                    var m = time.getMonth()+1;
                    var d = time.getDate();
                    var h = time.getHours();
                    var mm = time.getMinutes();
                    var s = time.getSeconds();
                    var add0=function (m) {
                        return m<10?'0'+m:m
                    }
                    return y+'-'+add0(m)+'-'+add0(d)+' '+add0(h)+':'+add0(mm)+':'+add0(s);
                }},
            {label: '拼团规则', name: 'groupPurchaseRule', index: 'groupPurchaseRule', width: 150},
            {
                label: '活动状态', name: 'status', index: 'status', width: 80,align:"center", formatter: function (value) {
                    return value === 0 ?
                        '<span class="label  label-success">启用</span>' :
                        '<span class="label label-danger">禁用</span>';
                }
            }
        ]
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        gift: {
            activityImg:'',//活动图片
        },
        chooseGift:{}, //选中的礼包类型全部信息
        giftTypeId:null, //选中的礼包类型
        giftTypes:[],  //所有礼包兑换类型
        adposition:{},
        ruleValidate: {
            // giftTypeId: [
            //     {required: true, message: '名称不能为空', trigger: 'blur',type:'number'}
            // ],
            // cardNumber: [
            //     {required: true, message: '卡号不能为空', trigger: 'blur'}
            // ],
            // exchangeCode: [
            //     {required: true, message: '兑换码不能为空', trigger: 'blur'}
            // ]

        },
        q: {
            param: ''
        }
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.gift = {width: 200, height: 750};

            //获取到类型数据，并显示
            vm.getGiftType()
        },
        //批量导入
        importAll:function(){

        },
        update: function (event) {
            var id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";
            vm.getInfo(id)
        },
        giftTypeChange:function(value){
            console.log(value,"选中的类型")
            var chooseGift=vm.giftTypes.filter(item=>{
                return item.id==value
            })

        },
        saveOrUpdate: function (event) {
            var url = vm.gift.id == null ? "../groupPurchase/save" : "../groupPurchase/update";

            Ajax.request({
                type: "POST",
                url: url,
                contentType: "application/json",
                params: JSON.stringify(vm.gift),
                successCallback: function () {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });


        },
        del: function (event) {
            var ids = getSelectedRows("#jqGrid");
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    type: "POST",
                    url: "../groupPurchase/delete",
                    contentType: "application/json",
                    params: JSON.stringify(ids),
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
        getInfo: function (id) {
            //获取礼包类型数据
            vm.getGiftType()
            Ajax.request({
                url: "../groupPurchase/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.gift = r.groupPurchaseEntity;
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'giftName': vm.q.param},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
        handleActivityStartDate:function(time){
            vm.activityStartDate=time
        },
        handleActivityEndDate:function(time){
            vm.activityEndDate=time
        },
        //获取礼包类型数据
        getGiftType:function () {
            Ajax.request({
                url: "../groupPurchase/list?limit=99999999&page=1&order=asc&sidx",
                type: "GET",
                contentType: "application/json",
                successCallback: function (r) {
                    console.log(r,'1111')
                    vm.giftTypes = r.page.list;
                }
            });
        },
        handleFormatError: function (file) {
            this.$Notice.warning({
                title: '文件格式不正确',
                desc: '文件 ' + file.name + ' 格式不正确，请上传 jpg 或 png 格式的图片。'
            });
        },
        handleMaxSize: function (file) {
            this.$Notice.warning({
                title: '超出文件大小限制',
                desc: '文件 ' + file.name + ' 太大，不能超过 2M。'
            });
        },
        handleSuccess: function (res, file) {
            vm.gift.activityImg = file.response.url;
            vm.$forceUpdate()
            // console.log(vm.gift,'123')
        },
        eyeImage: function () {
            var url = vm.gift.activityImg;
            eyeImage(url);
        }

    }
});